package com.baidumap.administrator.happystudy.module.splash.activity;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.ui.LoginActivity;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.socks.library.KLog;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;


/**
 * 描述：
 * 作者：xiaoxing on 17/4/20 13:15
 * 邮箱：2235445233@qq.com
 */

/**
 * 描述：启动动画
 * 作者：徐小星 on 2016/7/20 13:26
 * 邮箱：xx@yougudongli.com
 */
public class Activity_Animation extends AppBaseActivity {
    public static final int ANIMATION_TIME = 6000;
    @Bind(R.id.iv_animation_logo)
    ImageView ivAnimationLogo;
    @Bind(R.id.tv_company)
    TextView tv_company;
    @Bind(R.id.tl_an)
    RelativeLayout tl_an;

 
    private SimpleDraweeView dvWelcome;
    DraweeController draweeController;

    private Handler hd = new MyHandler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void initView(View view) {
        super.initView(view);
        tl_an.getBackground().setAlpha(255);
        //定时器延迟发送
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                Message msg = new Message();
                msg.what = 1; // 用户自定义的一个值，用于标识不同类型的消息
                hd.sendMessage(msg); // 发送消息

            }
        }, 1100);
        dvWelcome = (SimpleDraweeView) findViewById(R.id.dv_welcome);
        dvWelcome.setController(draweeController);

    }


    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1: // 接受到消息之后，对UI控件进行修改
                    tl_an.getBackground().setAlpha(0);
                    ivAnimationLogo.setVisibility(View.VISIBLE);
                    tv_company.setVisibility(View.GONE);

                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void doBussinessBeforeOnCreate() {
        super.doBussinessBeforeOnCreate();
        Fresco.initialize(this);
        /**
         * 下面是主要代码：
         */
        draweeController = Fresco.newDraweeControllerBuilder()
                .setAutoPlayAnimations(true)//自动播放动画
//                .setUri(Uri.parse("http://s1.dwstatic.com/group1/M00/9B/31/468f0fc71e49479bf021c64cefee2882.gif"))//路径
                .setUri(Uri.parse("asset://com.baidumap.administrator.happystudy/welcome1.gif"))//路径
                .build();
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 去掉界面任务条
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }


    @Override
    public int bindLayout() {
        return R.layout.activity_animation;
    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setTitleHidden();
        begin_animation();

    }

    /**
     * 开始动画
     */
    private void begin_animation() {

        //图片渐变模糊度始终
        AlphaAnimation aa = new AlphaAnimation(1.0f, 1.0f);
        //渐变时间
        aa.setDuration(ANIMATION_TIME);
        //展示图片渐变动画
        ivAnimationLogo.startAnimation(aa);

        //渐变过程监听
        aa.setAnimationListener(new Animation.AnimationListener() {

            /**
             * 动画开始时
             */
            @Override
            public void onAnimationStart(Animation animation) {
                KLog.e("动画开始..");
            }

            /**
             * 重复动画时
             */
            @Override
            public void onAnimationRepeat(Animation animation) {
                KLog.e("动画重复...");
            }

            /**
             * 动画结束时
             */
            @Override
            public void onAnimationEnd(Animation animation) {
                KLog.e("动画结束...");

//                if (sHelper.getBoolean(Constant.IS_FIRST, true)) {
                sHelper.putBoolean(Constant.IS_FIRST, false);
//                    getOperation().startActivity(Activity_Guide.class);
//                } else {
                getOperation().startActivity(LoginActivity.class);
                finish();
//                }


            }
        });
    }
}